﻿Imports System.IO
Imports System.Drawing

Public Class SelectFile
  Const icoOpen As Integer = 0
  Const icoClose As Integer = 1
  Const icoFile As Integer = 2
  Private _folder As String = ""
  Private _mode As Mode = Mode.FilesAndFolders

  Public Enum Mode
    FoldersOnly
    FilesAndFolders
  End Enum

  Public Sub New()
    Cursor.Current = Cursors.WaitCursor
    Me.InitializeComponent()
    Me.MenuItemOK.Text = Language.GetLanguageString("OK")
    Me.MenuItemCancel.Text = Language.GetLanguageString("Cancel")
    Me.InputPanelMain_EnabledChanged(Me, EventArgs.Empty)
  End Sub

  Public Sub New(ByVal newMode As Mode, ByVal title As String)
    Me.new()
    Me.LabelTitle.Text = title
    Me._mode = newMode
    If Me._mode = Mode.FilesAndFolders Then
      Me.TextBox1.Enabled = True
      Me.TextBox1.Visible = True
    Else
      Me.TextBox1.Enabled = False
      Me.TextBox1.Visible = False
    End If
  End Sub

  Public Property Title() As String
    Get
      Return Me.LabelTitle.Text
    End Get
    Set(ByVal value As String)
      Me.LabelTitle.Text = Title
    End Set
  End Property

  Public Property SelectMode() As Mode
    Get
      Return Me._mode
    End Get
    Set(ByVal value As Mode)
      Me._mode = value
      If Me._mode = Mode.FilesAndFolders Then
        Me.TextBox1.Enabled = True
        Me.TextBox1.Visible = True
        Cursor.Current = Cursors.WaitCursor
        Me.ClearTreeView()
        Cursor.Current = Cursors.Default
      Else
        Me.TextBox1.Enabled = False
        Me.TextBox1.Visible = False
        Cursor.Current = Cursors.WaitCursor
        Me.ClearTreeView()
        Cursor.Current = Cursors.Default
      End If
    End Set
  End Property

  Public ReadOnly Property Folder() As String
    Get
      If Me._folder.Trim.Length <= 0 Then
        Return "\"
      Else
        Return Me._folder.Trim
      End If
    End Get
  End Property

  Public ReadOnly Property Filename() As String
    Get
      If Me._mode = Mode.FilesAndFolders Then
        Return Me.TextBox1.Text.Replace("\", "").Trim
      Else
        Return ""
      End If
    End Get
  End Property

  Public ReadOnly Property Path() As String
    Get
      If Me._mode = Mode.FilesAndFolders Then
        Return StripExtraSlashesInPath(Me.Folder & "\" & Me.Filename)
      Else
        Return Me.Folder
      End If
    End Get
  End Property

  Private Sub SelectFile_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    Dim imgcol As New List(Of Image)

    ' Set the icons to the correct scaling
    Using g As Graphics = Me.CreateGraphics
      Dim scaleFactor As Single = g.DpiX / 96.0

      If scaleFactor <> 1.0 Then
        Me.ImageList1.ImageSize = New Size(Me.ImageList1.ImageSize.Width * scaleFactor, Me.ImageList1.ImageSize.Height * scaleFactor)
      End If
    End Using

    Me.ClearTreeView()
    Cursor.Current = Cursors.Default
  End Sub

  Private Sub ClearTreeView()
    TreeView1.Nodes.Clear()
    Dim node As New TreeNode
    Try
      ' create the root node
      node.ImageIndex = icoClose
      node.SelectedImageIndex = icoOpen
      node.Text = "\"
      TreeView1.Nodes.Add(node)
      ' Add the dummy node
      node.Nodes.Add("")
      node.Expand()
    Catch ex As Exception
      ErrorLog.WriteToErrorLog("SelectFile :: " & ex.ToString)
    End Try
  End Sub

  Private Sub TreeView1_BeforeExpand(ByVal sender As Object, ByVal e As TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
    Cursor.Current = Cursors.WaitCursor
    ' if leaf node then exit
    If e.Node.ImageIndex = icoFile Then Return
    ' remove the dummy node and display 
    ' the subdirectories and files
    e.Node.Nodes.Clear()  ' clears all the nodes and...
    DisplayChildNodes(e.Node) ' create the nodes again
    ' change the icon for this node to open
    If e.Node.GetNodeCount(False) > 0 Then
      e.Node.ImageIndex = icoClose
      e.Node.SelectedImageIndex = icoOpen
    End If
    Cursor.Current = Cursors.Default
  End Sub

  Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
    If e.Node.ImageIndex <> icoFile Then
      Me._folder = StripExtraSlashesInPath(e.Node.FullPath)
    Else
      Me._folder = Microsoft.VisualBasic.Left(e.Node.FullPath, e.Node.FullPath.Length - e.Node.Text.Length)
      Me.TextBox1.Text = e.Node.Text
    End If
  End Sub

  Private Sub DisplayChildNodes(ByVal parentNode As System.Windows.Forms.TreeNode)
    Dim FS As New DirectoryInfo(StripExtraSlashesInPath(parentNode.FullPath))

    Dim sl As New List(Of String)
    Dim sa() As String

    For Each di As DirectoryInfo In FS.GetDirectories()
      sl.Add(di.Name)
    Next
    sa = sl.ToArray
    Array.Sort(sa)

    For Each d As String In sa
      Dim tn As New TreeNode
      tn.Text = d
      tn.ImageIndex = icoClose
      tn.SelectedImageIndex = icoOpen
      parentNode.Nodes.Add(tn)
      ' add the dummy sub node
      tn.Nodes.Add("")
    Next

    sl.Clear()

    If Me._mode = Mode.FilesAndFolders Then
      For Each fi As FileInfo In FS.GetFiles()
        sl.Add(fi.Name)
      Next
      sa = sl.ToArray
      Array.Sort(sa)

      For Each f As String In sa
        Dim tn As New TreeNode
        tn.Text = f
        tn.ImageIndex = icoFile
        tn.SelectedImageIndex = icoFile
        parentNode.Nodes.Add(tn)
      Next
    End If
  End Sub

  Private Sub MenuItemOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemOK.Click
    Me.DialogResult = Windows.Forms.DialogResult.OK
    Me.Close()
  End Sub

  Private Sub MenuItemCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemCancel.Click
    Me.DialogResult = Windows.Forms.DialogResult.Cancel
    Me.Close()
  End Sub

  Private Sub InputPanelMain_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InputPanelMain.EnabledChanged
    If Me.InputPanelMain.Enabled Then
      Me.PanelPushUp.Height = Me.InputPanelMain.Bounds.Height
    Else
      Me.PanelPushUp.Height = 0
    End If
  End Sub
End Class